// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

package client;

import ocsf.client.*;
import common.*;
import entity.*;

import java.io.*;

/**
 * This class overrides some of the methods defined in the abstract
 * superclass in order to give more functionality to the client.
 *
 * @author Omri Barda		039725890
 * @author Amit Joseph		034608547
 * @author Gilad Shpigel  	300162393
 * @author Elad Elbaz		040539959
 * @author Natali Kazav		038109401
 * @version May 2012
 */
public class ChatClient extends AbstractClient
{
  //Instance variables **********************************************
  
  /**
   * The interface type variable.  It allows the implementation of 
   * the display method in the client.
   */
  ChatIF clientUI; 

  
  //Constructors ****************************************************
  
  /**
   * Constructs an instance of the chat client.
   *
   * @param host The server to connect to.
   * @param port The port number to connect on.
   * @param clientUI The interface type variable.
   */
  
  public ChatClient(String host, int port, ChatIF clientUI) 
    throws IOException 
  {
    super(host, port); //Call the superclass constructor
    this.clientUI = clientUI;
    openConnection();
  }

  
  //Instance methods ************************************************
    
  /**
   * This method handles all data that comes in from the server.
   *
   * @param msg The message from the server.
   */
  public void handleMessageFromServer(Object msg) 
  {
    clientUI.display(msg.toString());
  }

  /**
   * This method handles all data coming from the UI            
   *
   * @param message The message from the UI.    
   */
  public void handleMessageFromClientUI(Object message)
  {
    try
    {
    	sendToServer(message.toString());
    }
    catch(IOException e)
    {
      clientUI.display
        ("Could not send message to server.  Terminating client.");
      quit();
    }
  }
  
  /**
   * This method terminates the client.
   */
  public void quit()
  {
    try
    {
      closeConnection();
    }
    catch(IOException e) {}
    System.exit(0);
  }
  
  
	/**
	 * Preparing operation to send to server.
	 *
	 * @param Question to execution.
	 * @param Operation of the query
	 */	

  public void handleQuestionFromClient(Question q, int opr) 
  {
	  String query = null;
	  switch(opr)
	  {
	  		case Question.CREATE:
	  			String currentDate=new String((q.getqDate().getYear()+1900) + "-" + (q.getqDate().getMonth()+1) + "-" + q.getqDate().getDate());
	  		  			
	  			query = new String ("INSERT INTO `aes_prototype`.`<<entity>> - question`(`QuestionID`,`QuestionText`,`QuestionDate`,`QuestionCreator`,`QuestionInstructions`,`CorrectAnswer`,`AnswerA`,`AnswerB`,`AnswerC`) VALUES ('" +
	  					q.getqID() + "', '" + q.getqText() + "', '" + currentDate + "', '" + q.getqCreator() + "', '" + q.getqInstructions() + "', '" + q.getCorrectAns() + "', '" + q.getAnswerA() + "', '" + q.getAnswerB() + "', '" + q.getAnswerC() +"');");
	  			break;

	  		case Question.DELETE:
	  			query = new String ("DELETE FROM `aes_prototype`.`<<entity>> - question` WHERE QuestionID = " + q.getqID() + ";");
	  			break;
	  			
	  		case Question.GET:
	  			query = new String ("SELECT * FROM `aes_prototype`.`<<entity>> - question` Q WHERE Q.QuestionID like " + Integer.parseInt(q.getqID()) + ";");
	  			break;	
	  		default:
	  			Perror.pFatalError("Invalid operation!");
	  			break;
	  }
	  
	  try
	  	{
		  sendToServer(query);
	  	}
	    catch(IOException e)
	    {
	      Perror.pFatalError("Could not send query to server, please contact your server administrator.");
	    }
	
  }
}
//End of ChatClient class
